package service

import (
	"fmt"
	"go.uber.org/zap"
	"iot-base/common/cache"
	"iot-base/common/ctrltype"
	"iot-base/common/logger"
	"iot-base/common/notify"
	"regexp"
	"strconv"
	"strings"
	"time"

	"errors"
)

type O3 struct {
}

type O3Logic struct {
}

func NewO3Logic() *O3Logic {
	return &O3Logic{}
}

func (this *O3Logic) O3(id string, thing *cache.ThingInfo, v interface{}, round int64) (interface{}, error) {
	//ip := thing.GetThingParam("ip", "")
	//port := thing.GetThingParam("port", "")
	//co3Str, err := SynHub.SendStrData(ip+":"+port, "oo3chr$")
	b, err := RequestData(thing, false, []byte("oo3chr$"))
	co3Str := string(b)
	if strings.Contains(co3Str, "error") {
		logger.Log.Error("O3 service.SynHub.SendStrData err:" + co3Str)
		fmt.Println("oo3chr$", co3Str)
		return nil, err
	}
	logger.Log.Info("Co oo3chr$ " + co3Str)
	fmt.Println("oo3chr$ info:=", co3Str, err)
	if err != nil {
		logger.Log.Error("O3 service.SynHub.SendStrData err:", zap.Error(err))
		return nil, err
	}
	co3Arr := strings.Split(co3Str, "=")
	var status float64 = 1
	switch co3Arr[0][0:1] {
	case "*":
		status = 2
	case "#":
		status = 3
	}
	reg := regexp.MustCompile("[\\-]?[\\d]+\\.?[\\d]+")
	data := reg.Find([]byte(co3Arr[1]))
	now := time.Now().Unix()
	co3, _ := strconv.ParseFloat(string(data), 64)
	notify.RawData(thing.Thing.UID, "o3", "", 0, co3, now, now)
	notify.RawData(thing.Thing.UID, "status", "", 0, status, now, now)
	notify.RawData(thing.Thing.UID, "o3"+"ValueStatus", "", 0, fmt.Sprintf("%s%v", co3Arr[0][0:1], co3), now, now)

	// 获取详细状态.
	//oo3twc, err := SynHub.SendStrData(ip+":"+port, "oo3twc$")
	b, err = RequestData(thing, false, []byte("oo3twc$"))
	oo3twc := string(b)
	fmt.Println("oo3twc", oo3twc, err)
	if err != nil {
		logger.Log.Error("O3 service.SynHub.SendStrData err:", zap.Error(err))
		return nil, err
	}
	logger.Log.Info("O3 oo3twc " + oo3twc)
	if strings.Contains(oo3twc, "error") {
		logger.Log.Error("O3 oo3twc$ service.SynHub.SendStrData err:" + oo3twc)
		fmt.Println("oo3twc$", oo3twc)
		return nil, err
	}

	oo3twc = oo3twc[:len(oo3twc)-1]
	oo3twcArr := strings.Split(oo3twc, ",")
	// 上报数据.
	ReportDataFloat64(thing.Thing.UID, oo3twcArr, O3Field)
	return nil, nil
}

var O3Field = map[int]string{
	1: "pmt_v",
	2: "canbi_v",
	3: "power",
	4: "press",
	5: "flow",
	6: "temp",
	7: "boxtemp",
	8: "uvtemp",
}

func (this *O3Logic) O3Sv(pCmd *ctrltype.CtrlCmd, tingInfo *cache.ThingInfo) (map[string]interface{}, error) {
	//ip := tingInfo.GetThingParam("ip", "")
	//port := tingInfo.GetThingParam("port", "")
	var requestMap map[string]interface{}
	requestMap = make(map[string]interface{})
	switch pCmd.ID {
	case "ozeros":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "ozeros$")
		b, err := RequestData(tingInfo, true, []byte("ozeros$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "ozeroe":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "ozeroe$")
		b, err := RequestData(tingInfo, true, []byte("ozeroe$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "ozeroq":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "ozeroq$")
		b, err := RequestData(tingInfo, true, []byte("ozeroq$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
		}
		return requestMap, nil
	case "ospans":
		concentration, ok := pCmd.Params["concentration"].(string)
		if !ok {
			return nil, errors.New("concentration type is err")
		}
		if len(concentration) < 3 {
			for i := 0; i < 4-len(concentration); i++ {
				concentration = "0" + concentration
			}
		}
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "ospans "+concentration+"$")
		b, err := RequestData(tingInfo, true, []byte("ospans "+concentration+"$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "ospane":
		concentration, ok := pCmd.Params["concentration"].(string)
		if !ok {
			return nil, errors.New("concentration type is err")
		}
		if len(concentration) < 3 {
			for i := 0; i < 4-len(concentration); i++ {
				concentration = "0" + concentration
			}
		}
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "ospane "+concentration+"$")
		b, err := RequestData(tingInfo, true, []byte("ospane "+concentration+"$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "ospanq":
		concentration, ok := pCmd.Params["concentration"].(string)
		if !ok {
			return nil, errors.New("concentration type is err")
		}
		if len(concentration) < 3 {
			for i := 0; i < 4-len(concentration); i++ {
				concentration = "0" + concentration
			}
		}
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "ospanq "+concentration+"$")
		b, err := RequestData(tingInfo, true, []byte("ospanq "+concentration+"$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "status":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "oo3chr$")
		b, err := RequestData(tingInfo, true, []byte("oo3chr$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "error") {
			logger.Log.Error("o3 service.SynHub.SendStrData err:" + readbuffer)
			fmt.Println("oo3chr$", readbuffer)
			return nil, err
		}
		logger.Log.Info("o3 oo3chr$ info:=" + readbuffer)
		fmt.Println("oo3chr$ info:=", readbuffer, err)
		if err != nil {
			logger.Log.Error("o3 service.SynHub.SendStrData err:", zap.Error(err))
			return nil, err
		}
		switch readbuffer[:1] {
		case "*":
			requestMap["msg"] = "2"
		case "#":
			requestMap["msg"] = "3"
		default:
			requestMap["msg"] = "1"
		}
		return requestMap, nil
	case "value":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "oo3chr$")
		b, err := RequestData(tingInfo, true, []byte("oo3chr$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "error") {
			logger.Log.Error("o3 service.SynHub.SendStrData err:" + readbuffer)
			fmt.Println("oo3chr$", readbuffer)
			return nil, err
		}
		logger.Log.Info("o3 oo3chr$ info:=" + readbuffer)
		fmt.Println("oo3chr$ info:=", readbuffer, err)
		if err != nil {
			logger.Log.Error("o3 service.SynHub.SendStrData err:", zap.Error(err))
			return nil, err
		}
		data := strings.Split(readbuffer, "=")
		if len(data) != 2 {
			return nil, errors.New("return data is err:" + readbuffer)
		}
		data[1] = strings.Replace(data[1], "$", "", -1)
		if data[1][0] == '*' || data[1][0] == '#' {
			requestMap["value"] = data[1][1:]
		} else {
			requestMap["value"] = data[1]
		}
		return requestMap, nil
	case "his":
		start, ok := pCmd.Params["start"].(string)
		if !ok {
			return nil, errors.New("start type is err")
		}
		end, ok := pCmd.Params["end"].(string)
		if !ok {
			return nil, errors.New("end type is err")
		}
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "oo3his"+start+" "+end+"$")
		b, err := RequestData(tingInfo, true, []byte("oo3his"+start+" "+end+"$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "error") {
			logger.Log.Error("oo3 service.SynHub.SendStrData err:" + readbuffer)
			fmt.Println("oo3his", readbuffer)
			return nil, err
		}
		requestMap["result"] = readbuffer
		return requestMap, nil
	default:
		return nil, errors.New("no service")
	}

}
